package com.wy.controller.wy;

import com.wy.model.sys.PageResult;
import com.wy.model.sys.VisitorEntity;
import com.wy.service.wy.VisitorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/visitors")
public class VisitorController {
    private Logger logger = LoggerFactory.getLogger(VisitorController.class);
    @Resource(name ="visitorServiceImpl")
    private VisitorService visitorService;

    /**
     * 获取访客记录列表
     * @param pageSize
     * @param page
     * @param visitorName
     * @param household
     * @param visitTimeStart
     * @param visitTimeEnd
     * @return
     */
    @GetMapping("")
    public PageResult visitorList(@RequestParam(value = "pageSize",required = false) int pageSize,
                                  @RequestParam(value = "page",required = false) int page,
                                  @RequestParam(value = "visitorName",required = false) String visitorName,
                                  @RequestParam(value = "household",required = false) String household,
                                  @RequestParam(value = "visitTimeStart",required = false) String visitTimeStart,
                                  @RequestParam(value = "visitTimeEnd",required = false) String visitTimeEnd ){
        PageResult pageResult = new PageResult();
        pageResult.setData(visitorService.visitorList(pageSize,page*pageSize,visitorName,household,visitTimeStart,visitTimeEnd));
        pageResult.setTotalCount(visitorService.visitorSize(pageSize,page*pageSize,visitorName,household,visitTimeStart,visitTimeEnd));
        return  pageResult;


    }
    /**
     * 新增访客记录
     * @param visitorEntity
     * @return
     */
    @PostMapping("/visitor")
    public VisitorEntity insertVisitor(@RequestBody VisitorEntity visitorEntity){
        visitorService.insertVisitor(visitorEntity);
        logger.debug("the method ending");
        return visitorEntity;
    }

    /**
     * 修改访客记录
     * @param visitorEntity
     * @param id
     * @return
     */
    @PutMapping("{id}")
    public VisitorEntity updateVisitor(@RequestBody VisitorEntity visitorEntity , @PathVariable int id){
        if(visitorEntity.getId() == id){
            visitorService.updateVisitor(visitorEntity);
        }
        logger.debug("update method ending");
        return visitorEntity;
    }
}
